package com.lmsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmsservice.common.R;
import com.lmsservice.entity.Records;
import com.lmsservice.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @GetMapping("/page")
    public R<Page> getPage(Integer page, Integer pageSize, Long id, String userName,String bookName){

        Page<Records> recordPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(id!=0,Records::getId,id)
                .like(!Objects.equals(userName, ""),Records::getUserName,userName)
                .like(!Objects.equals(bookName, ""),Records::getBookName,bookName);
        recordService.page(recordPage,queryWrapper);
        return R.success(recordPage);
    }

//    public R<List<Records>> getlist(){
//
//    }

    @PostMapping
    public R<String> addrecords(@RequestBody Records records){
        recordService.save(records);
        return R.success("添加成功");
    }

    @DeleteMapping
    public R<String> deleterecords(@RequestBody Records records){

        Records records1 = recordService.getById(records.getId());
        if (records1.getStatus()==0){
            return R.error("删除失败，图书未归还");
        }
        recordService.removeById(records);
        return R.success("删除成功");

    }

    @PutMapping
    public R<String> updaterecords(@RequestBody Records records){
        recordService.updateById(records);
        return R.success("修改成功");
    }

    @GetMapping
    public R<List<Records>> getList(Long id){
        LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=0,Records::getUserId,id);
        List<Records> list = recordService.list(queryWrapper);
        return R.success(list);
    }
}
